package com.system.insurance.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.common.result.Result;
import com.system.insurance.entity.AppletUser;
import com.system.insurance.entity.ServerApplyFor;
import com.system.insurance.service.AppletUserService;
import com.system.insurance.service.ServerApplyForService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author WS
 * @since 2024-05-09
 */
@Api(tags = "服务申请")
@RestController
@RequestMapping("/insurance/server-apply-for")
public class ServerApplyForController {


    @Autowired
    private ServerApplyForService serverApplyForService;


    @Autowired
    private AppletUserService appletUserService;


    @ApiOperation(value = "后台：所有服务申请")
    @GetMapping("/getList")
    public Result<List<ServerApplyFor>> getList(){
        List<ServerApplyFor> list = serverApplyForService.list();
       return Result.ok(list);
    }

    @ApiOperation(value = "添加服务申请")
    @PostMapping("/saveServerApplyFor")
    public Result<ServerApplyFor> saveServerApplyFor(@RequestBody ServerApplyFor carClassify){

        carClassify.setCreateTime(LocalDateTime.now());
        boolean save = serverApplyForService.save(carClassify);

        if (save){
            return Result.ok(carClassify).message("添加成功");
        }else {
            return Result.ok(carClassify).message("添加失败");
        }
    }





    @ApiOperation(value = "修改服务申请")
    @PostMapping("/updateServerApplyFor")
    public Result<ServerApplyFor> updateServerApplyFor(@RequestBody ServerApplyFor carClassify){

        carClassify.setUpdateTime(LocalDateTime.now());
        boolean update = serverApplyForService.updateById(carClassify);

        if (update){
            return Result.ok(carClassify).message("修改成功");
        }else {
            return Result.ok(carClassify).message("修改失败");
        }
    }


    @ApiOperation(value = "审核服务申请状态成功")
    @PostMapping("/auditServerApplyFor")
    public Result<ServerApplyFor> auditServerApplyFor(@RequestBody ServerApplyFor carClassify){

            carClassify.setStart(1);
            boolean update = serverApplyForService.updateById(carClassify);

            AppletUser byId = appletUserService.getById(carClassify.getAppletUserId());
            byId.setCompany(carClassify.getCompany());
            byId.setName(carClassify.getName());
            byId.setIdNumber(carClassify.getIdNumber());
            byId.setGreenStudio(carClassify.getAvatar());
            byId.setStart(1);
            appletUserService.updateById(byId);

            if (update) {
                return Result.ok(carClassify).message("修改成功");
            } else {
                return Result.ok(carClassify).message("修改失败");
            }

    }


    @ApiOperation(value = "服务申请驳回")
    @PostMapping("/rejectServe")
    public Result rejectServe(@RequestBody ServerApplyFor carClassify){
        carClassify.setStart(2);
        boolean update = serverApplyForService.updateById(carClassify);

        if (update) {
            return Result.ok(carClassify).message("修改成功");
        } else {
            return Result.ok(carClassify).message("修改失败");
        }
    }

    @ApiOperation(value = "删除服务申请")
    @PostMapping("delectServerApplyFor/{id}")
    public Result delectServerApplyFor(@PathVariable Integer id){

        boolean b = serverApplyForService.removeById(id);

        if (b){
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }


    @ApiOperation(value = "分页查询服务申请")
    @GetMapping("/{page}/{limit}")
    public Result<IPage<ServerApplyFor>> index(@PathVariable Long page, @PathVariable Long limit, ServerApplyFor carClassify){

        Page<ServerApplyFor> recommendPage = new Page<>(page,limit);

        LambdaQueryWrapper<ServerApplyFor> wrapper = new LambdaQueryWrapper<>();

        Integer id = carClassify.getId();

        if (id!=null){
            wrapper.eq(ServerApplyFor::getId,id);
        }

        IPage<ServerApplyFor> iPage = serverApplyForService.page(recommendPage,wrapper);

        return Result.ok(iPage);
    }


}

